Typical HAL DTS use cases for migration

Here are the typical DTS use cases for HAL driver migration.


Use case: Get temperature with software/hardware trigger (polling)

Note

In HAL1, there is no status flag indicating “end of measurement” in polling mode. To detect completion, the user must explicitly enable the “End of measurement” interrupt flag via __HAL_DTS_ENABLE_IT(...) and then poll that flag.

In HAL2, this interrupt is enabled automatically by HAL_DTS_StartSensor(), and the driver uses its flag as a status indicator in HAL_DTS_PollForSensorTemperature().

Warning: When using polling mode, do not enable the NVIC interrupt request DTS_IRQn; only the peripheral interrupt flag is used for polling.

Sequence diagram

  @startuml
  participant "User Application" as User
  participant "HAL DTS Driver" as DTS #19DD3A

  == Process: Polling mode ==
  User -> DTS:HAL_DTS_Start()
  User <-- DTS: HAL_OK

  User -> DTS:__HAL_DTS_ENABLE_IT()
  note right
  <font color=red>The IT needs to be enabled in the polling mode
  end note

  alt Hardware trigger
  HW <- : External HW trigger
  else Software trigger
  |||
  end

  User -> DTS:__HAL_DTS_GET_FLAG()
  HW-->DTS: End of measurement
  HW<--DTS:

  User <-- DTS: HAL_OK
  note left
  <font color=green>Temperature is ready to retrieve
  end note
  User -> DTS:HAL_DTS_GetTemperature()
  User <-- DTS: HAL_OK

  User -> DTS:HAL_DTS_Stop()

  User <-- DTS: HAL_OK


  @enduml

Code snippet

HAL1

HAL2

DTS_HandleTypeDef hdts;
int32_t temp;

hdts.Init.TriggerInput = DTS_TRIGGER_HW_NONE /* DTS_TRIGGER_LPTIMER2 for HW trigger*/;
HAL_DTS_Init(&hdts);

__HAL_DTS_ENABLE_IT(&hdts, DTS_IT_TS1_ITE);

HAL_DTS_Start(&hdts);


__HAL_DTS_GET_FLAG(&hdts, DTS_FLAG_TS1_ITE);

HAL_DTS_GetTemperature(&hdts, &temp);
HAL_DTS_Stop(&hdts);
hal_dts_handle_t hDTS;
int32_t temp;

HAL_DTS_Init(&hDTS, HAL_DTS);
hal_dts_config_t cfg = { .reference_clock = HAL_DTS_REFERENCE_CLOCK_PCLK,
                         .quick_measurement = HAL_DTS_QUICK_MEASUREMENT_DISABLE,
                         .high_speed_clock_division = 0 };
hal_dts_sensor_config_t sensor_cfg = { .trigger = HAL_DTS_TRIGGER_SOFTWARE, /* HAL_DTS_TRIGGER_LPTIM2_CH1 for HW trigger */
                                       .sampling_time = HAL_DTS_SAMPLING_TIME_1_CYCLE };
HAL_DTS_SetConfig(&hDTS, &cfg);
HAL_DTS_SetConfigSensor(&hDTS, HAL_DTS_SENSOR_1, &sensor_cfg);
HAL_DTS_StartSensor(&hDTS, HAL_DTS_SENSOR_1);
HAL_DTS_PollForSensorTemperature(&hDTS, HAL_DTS_SENSOR_1, 1000);
HAL_DTS_GetSensorTemperature(&hDTS, HAL_DTS_SENSOR_1, &temp);
HAL_DTS_StopSensor(&hDTS, HAL_DTS_SENSOR_1);

Key changes

  1. In HAL2, HAL_DTS_StartSensor() enables the end-of-measurement interrupt flag used by HAL_DTS_PollForSensorTemperature().

  2. In polling mode, do not enable DTS_IRQn; only the peripheral interrupt flag is polled.

  3. HAL2 uses per-sensor configuration and per-sensor start/stop APIs.

Use case: Get temperature with software/hardware trigger (interrupt mode)

Sequence diagram

  @startuml
  participant "User Application" as User
  participant "HAL DTS Driver" as DTS #19DD3A
  participant "NVIC" as NVIC
  participant "HW" as HW

  == Process: Non-Blocking mode ==

  User -> DTS:HAL_DTS_Start_IT()

  User <-- DTS: HAL_OK
  activate HW #BLUE
  HW <-- : External HW trigger
  note left
  <font color=blue>only with hardware trigger configuration
  end note
  loop
  NVIC <- HW : end of measurement interrupt
  DTS <- NVIC : HAL_DTS_IRQHandler
  User <- DTS: HAL_DTS_EndCallback or \n HAL_DTS_AsyncEndCallback
  User --> DTS:
  DTS --> NVIC :

  User -> DTS:HAL_DTS_GetTemperature()
  User <-- DTS: HAL_OK
  end
  User -> DTS:HAL_DTS_Stop_IT()
  destroy HW #BLUE
  User <-- DTS: HAL_OK
  @enduml

Code snippet

HAL1

HAL2

DTS_HandleTypeDef hdts;

hdts.Init.TriggerInput = DTS_TRIGGER_SOFTWARE; /* DTS_TRIGGER_LPTIMER2 for HW trigger*/
HAL_DTS_Init(&hdts);

HAL_DTS_Start_IT(&hdts);
HAL_DTS_GetTemperature(&hdts, &temp);
HAL_DTS_Stop_IT(&hdts);
hal_dts_handle_t hDTS;
HAL_DTS_Init(&hDTS, HAL_DTS);
hal_dts_sensor_config_t sensor_cfg;
int32_t temp;

sensor_cfg.trigger = HAL_DTS_TRIGGER_SOFTWARE; /* HAL_DTS_TRIGGER_LPTIM2_CH1 for HW trigger */
sensor_cfg.sampling_time = HAL_DTS_SAMPLING_TIME_1_CYCLE;
HAL_DTS_SetConfigSensor(&hDTS, HAL_DTS_SENSOR_1, &sensor_cfg);
HAL_DTS_StartSensor_IT(&hDTS, HAL_DTS_SENSOR_1);
HAL_DTS_GetSensorTemperature(&hDTS, HAL_DTS_SENSOR_1, &temp);
HAL_DTS_StopSensor_IT(&hDTS, HAL_DTS_SENSOR_1);

Key changes

  1. HAL2 uses per-sensor interrupt start/stop APIs (HAL_DTS_StartSensor_IT() and HAL_DTS_StopSensor_IT()).

  2. The HAL2 completion callback is consolidated as HAL_DTS_CpltCallback.

Related examples in HAL2

Example name

Brief

example_hal_dts_get_temperature_trigger_hw_it

Get die temperature using hardware trigger in interrupt (non-blocking) mode.


Use case: Detect temperature thresholds (non-blocking)

Note

In HAL2, threshold configuration is simplified: use the dedicated API HAL_DTS_SetSensorThresholds(), which accepts High and Low thresholds directly in degrees Celsius. This replaces the manual calculation required in HAL1.

You can check your reference manual to get the formula used in HAL1 to convert register values to degrees Celsius.

Sequence diagram

  @startuml
  participant "User Application" as User
  participant "HAL DTS Driver" as DTS #19DD3A
  participant "NVIC" as NVIC
  participant "HW" as HW
  == Process: non-blocking mode ==

  User -> DTS: HAL_DTS_Start_IT()

  User <-- DTS: HAL_OK

  loop
  alt Temperature is Under High threshold value / Over Low threshold value
      HW <- : External HW trigger
      note left
      <font color=blue>only with hardware trigger configuration
      end note
      note over HW
      <font color=red>Measuring ended without Threshold detection
      end note

  else Temperature is Over High threshold value / Under Low threshold value
      HW <- : External HW trigger
      note left
      <font color=blue>only with hardware trigger configuration
      end note

      NVIC <- HW : High or Low threshold detection interrupt
      DTS <- NVIC : HAL_DTS_IRQHandler
      alt When using PCLK as clock source
          User <- DTS: HAL_DTS_LowCallback \n(or HAL_DTS_HighCallback)
      else When using LSE as clock source
          User <- DTS: HAL_DTS_AsyncLowCallback \n(or HAL_DTS_AsyncHighCallback)
      end
      User --> DTS:
      DTS --> NVIC :
  end

  User -> DTS: HAL_DTS_GetTemperature()
  User <-- DTS: HAL_OK
  end

  User -> DTS: HAL_DTS_Stop_IT()
  User <-- DTS: HAL_OK
  @enduml

Code snippet

HAL1

HAL2

DTS_HandleTypeDef hdts;
int32_t temp;

HAL_DTS_Init(&hdts);
hdts.Init.HighThreshold = 100; /* register readable value that needs to be */
hdts.Init.LowThreshold = 10;   /* calculated to select the desired threshold in degrees Celsius */
/* For example: Temperature: 40
   register writable value: 60
   given: ramp coeff (r): 6080, Calibration frequency (r): 1875000Hz, T0: 3D, using LSE: 32000Hz
   These values depend on your specific device and configuration, check your reference manual for more details */
HAL_DTS_Start_IT(&hdts);
HAL_DTS_GetTemperature(&hdts, &temp);
HAL_DTS_Stop_IT(&hdts);
hal_dts_handle_t hDTS;
int32_t temp;

HAL_DTS_Init(&hDTS, HAL_DTS);
hal_dts_config_t cfg = { .reference_clock = HAL_DTS_REFERENCE_CLOCK_PCLK,
                         .quick_measurement = HAL_DTS_QUICK_MEASUREMENT_DISABLE,
                         .high_speed_clock_division = 0 };
hal_dts_sensor_config_t sensor_cfg = { .trigger = HAL_DTS_TRIGGER_SOFTWARE,
                                       .sampling_time = HAL_DTS_SAMPLING_TIME_1_CYCLE };
HAL_DTS_SetConfig(&hDTS, &cfg);
HAL_DTS_SetConfigSensor(&hDTS, HAL_DTS_SENSOR_1, &sensor_cfg);
/* Configure thresholds (degC) using dedicated API */
HAL_DTS_SetSensorThresholds(&hDTS, HAL_DTS_SENSOR_1, 100, 10);
/* Enable low threshold optional interrupt */
HAL_DTS_StartSensor_IT_Opt(&hDTS, HAL_DTS_SENSOR_1, HAL_DTS_OPT_IT_LOW_THRESHOLD);
HAL_DTS_GetSensorTemperature(&hDTS, HAL_DTS_SENSOR_1, &temp);
HAL_DTS_StopSensor_IT(&hDTS, HAL_DTS_SENSOR_1);

Key changes

  1. HAL2 configures thresholds in degrees Celsius using HAL_DTS_SetSensorThresholds().

  2. HAL2 enables optional threshold interrupts using HAL_DTS_StartSensor_IT_Opt().


Use case: Wakeup from Stop/Sleep mode at end of measurement

Sequence diagram

  @startuml
  participant "User Application" as User
  participant "HAL DTS Driver" as DTS #19DD3A
  participant "PWR" as PWR
  participant "NVIC" as NVIC
  participant "HW" as HW

  == Process: non-blocking mode ==

  User -> DTS:HAL_DTS_Start_IT()
  User <-- DTS: HAL_OK
  User -> DTS: __HAL_DTS_Enable_IT()


  User -> PWR**: HAL_PWREx_EnterSTOP0Mode()
  hnote across #FFAAAA
  <font color=red> ENTER IN STOP MODE
  end note
  HW <-: External HW trigger
  note over HW
  <font color=blue>DTS start measuring Temperature in <color red> **stop mode**</color>...
  end note
  note over HW
  <font color=green>DTS complete measuring Temperature
  end note

  hnote across #99FF99
  <font color=black> WAKEUP FROM STOP MODE
  end note
  User <-- PWR:
  NVIC <- HW: end of measurement interrupt
  DTS <- NVIC : HAL_DTS_IRQHandler
  User <- DTS: HAL_DTS_EndCallback or \n HAL_DTS_AsyncEndCallback

  User --> DTS:
  DTS --> NVIC :

  User -> DTS:HAL_DTS_GetTemperature()
  User <-- DTS: HAL_OK

  User -> DTS:HAL_DTS_Stop_IT()
  User <-- DTS:HAL_OK

  @enduml

Key changes

  1. HAL2 uses per-sensor interrupt start/stop APIs (HAL_DTS_StartSensor_IT() and HAL_DTS_StopSensor_IT()).

  2. The HAL2 completion callback is consolidated as HAL_DTS_CpltCallback.

Related examples in HAL2

Example name

Brief

example_hal_dts_wakeup_from_stop

Perform and handle DTS temperature measurement with interrupt while CPU is in STOP mode.